/**
 * 企业注册
 * @constructor
 */
function Enterprise() {

}

Enterprise.init = function () {
    // $('#text_animal').editableSelect({
    //     bg_iframe: true,
    //
    //     case_sensitive: false,
    //     items_then_scroll: 10 ,
    //     isFilter:false
    // });
    $("#mainModal").modal('show');
    $('#text_types').change(function () {
        if($('#text_types').val() == 0 || $('#text_types').val() ==2){
            $('#otherInfoContainer').hide();
        }else{
            $('#otherInfoContainer').show();
        }
    })

    $('#text_animal').change(function () {
        if($('#text_animal').val() > 8){
            $("#text_animal_name_show").show();
        }else{
            $("#text_animal_name_show").hide();
        }
        if($('#text_animal').val() <= 4){
            $('#livestockContainer').show(); //猪
            $('#livestockContainer1').hide(); //鸡
        }else if ($('#text_animal').val() > 4 && $('#text_animal').val() <9){
            $('#livestockContainer').hide();
            $('#livestockContainer1').show();
        }else if ($('#text_animal').val() == 9){
            $('#livestockContainer').show(); //猪
            $('#livestockContainer1').hide();
        }else {
            $('#livestockContainer').hide();
            $('#livestockContainer1').show();
        }
    })

    $('#text_cityCode').change(function () {
        $('#text_districtCode').empty();
        $('#text_townCode').empty();
        if($('#text_cityCode').val()){
            loadDistricts($('#text_cityCode').val());
        }
    })

    $('#text_districtCode').change(function () {
        $('#text_townCode').empty();
        if($('#text_districtCode').val()){
            loadTowns($('#text_districtCode').val());
        }
    })

    $('#showMap').click(function () {
        map.centerAndZoom($('#text_cityCode').find("option:selected").text().replace('市',''),11);
        $("#mainModal").modal('hide');
        $('#mapModal').modal('show');
    })


    loadCities();
    initMap();
}

var map = null;
var local = null;

function initMap() {
    map = new BMap.Map("allmap");
    map.enableScrollWheelZoom();
    map.centerAndZoom('福州',11);
    map.addEventListener("click",function(e){
        $('#location').val(e.point.lng+','+e.point.lat)
    });
    map.addControl(new BMap.NavigationControl());
    local = new BMap.LocalSearch(map, {
        renderOptions:{map: map}
    });
}

function theLocation(){
    var keywords = $("#text_keywords").val();
    if(keywords != ""){
        local.search(keywords);
    }
}

function mapOk() {
    closeMapModal();
    $('#text_location').val($('#location').val());
}

function closeMapModal() {
    $('#mapModal').modal('hide');
    $("#mainModal").modal('show');
}

function loadCities() {
    $.ajax({
        url: "/city/tableSearch",
        type:"post",
        data:JSON.stringify({pageSize: 100}),
        contentType:"application/json",
        dataType:"json",
        success:function(data){
            initCitySelect(data.data);
            if(data.data[0]){
                loadDistricts(data.data[0].city_id)
            }
        }
    });
}


function loadDistricts(cityId) {
    $.ajax({
        url: "/district/tableSearch",
        type:"post",
        data:JSON.stringify({city_id:cityId,pageSize: 100}),
        contentType:"application/json",
        dataType:"json",
        success:function(data){
            initDistrictSelect(data.data);
            if(data.data[0]){
                loadTowns(data.data[0].district_id)
            }
        }
    });
}


function loadTowns(districtId) {
    $.ajax({
        url: "/town/tableSearch",
        type:"post",
        data:JSON.stringify({district_id:districtId,pageSize: 100}),
        contentType:"application/json",
        dataType:"json",
        success:function(data){
            initTownSelect(data.data);
        }
    });
}

function initCitySelect(data) {
    $('#text_cityCode').empty();
    $.each(data,function (index,item) {
        $('#text_cityCode').append("<option value='"+item.city_id+"'>"+item.city_name+"</option>");
    })
}

function initDistrictSelect(data) {
    $('#text_districtCode').empty();
    $.each(data,function (index,item) {
        $('#text_districtCode').append("<option value='"+item.district_id+"'>"+item.district_name+"</option>");
    })
}

function initTownSelect(data) {
    $('#text_townCode').empty();
    $.each(data,function (index,item) {
        $('#text_townCode').append("<option value='"+item.town_id+"'>"+item.town__name+"</option>");
    })
}

var Action="add";
var enterpriseId=0;
var pxt="/enterprise"; //控制器名称
//增加/修改数据保存****************************
function saveRecord() {
    if(!validateForm()) return false;
//创建传递的参数
    var postdata = {
        enterpriseId: enterpriseId,
        action:Action,
        enterpriseName: $("#text_enterpriseName").val(),
        types: $("#text_types").val(),
        userName: $("#text_userName").val(),
        phone: $("#text_phone").val(),
        cityCode: $("#text_cityCode").val(),
        districtCode: $("#text_districtCode").val(),
        townCode: $("#text_townCode").val(),
        address: $("#text_address").val(),
        fullAddress: $("#text_cityCode").find("option:selected").text()+ $("#text_districtCode").find("option:selected").text()+ $("#text_townCode").find("option:selected").text()+ $("#text_address").val(),
        investment: $("#text_investment").val(),
        area: $("#text_area").val(),
        longitude: $("#text_location").val().split(',')[0],
        latitude: $("#text_location").val().split(',').length >0 ? $("#text_location").val().split(',')[1]:'',
        idCard: $("#text_iDCard").val(),
        identification: $("#text_identification").val(),
        certificate: $("#text_certificate").val(),
        certificateCode: $("#text_certificateCode").val(),
        scope: $("#text_scope").val(),
        animal: $("#text_animal").val()>7 ? $("#text_animal_name").val() : $("#text_animal").find("option:selected").text(),
        species: $("#text_species").val(),
        reserve: $("#text_reserve").val(),
        menstruation: $("#text_menstruation").val(),
        commodity: $("#text_commodity").val(),
        livestock: $("#text_livestock").val(),
        animalTypes: $("#text_animal").val()==1 || $("#text_animal").val() == 2 || $("#text_animal").val()==3 || $("#text_animal").val()==4 || $("#text_animal").val()==9 ? 1 : 0,
        loginID: $("#text_loginID").val(),
        password: $("#text_password").val(),
        lactation: $("#text_lactation").val(),
        conservation: $("#text_conservation").val(),
    };

    if(postdata.types == 0 || postdata.types == 2){ //屠宰企业/无害化企业，需移除养殖企业相关信息
        delete postdata.identification;
        delete postdata.certificate;
        delete postdata.certificateCode;
        delete postdata.scope;
        delete postdata.animal;
        delete postdata.species;
        delete postdata.reserve;
        delete postdata.menstruation;
        delete postdata.commodity;
        delete postdata.livestock;
        delete postdata.animalTypes;
        delete postdata.lactation;
        delete postdata.conservation;
    }
    else{
        if(postdata.animalTypes == 0){ //养家禽的需移除牲畜信息
            delete postdata.species;
            delete postdata.reserve;
            delete postdata.menstruation;
            delete postdata.commodity;
            delete postdata.lactation;
            delete postdata.conservation;
        }else{
            delete postdata.livestock;
        }
    }

    $.ajax({
        url:pxt+(Action == "add" ? "/save":"/update"),
        type:"post",
        data:JSON.stringify(postdata),
        contentType:"application/json",
        dataType:"json",
        success:function(data){
            if (data.code === "200") {
                $('#save').attr("disabled","disabled");
                swal("操作成功!", "", "success");
            } else {
                swal("操作失败!", data.message, "error");
            }
        }
    });
}

jQuery.validator.addMethod("passwordValidator", function(value, element) {
   var pwd = $('#text_password').val();
    var pwdConfirm = $('#text_passwordConfirm').val();
    if(pwd!='' && pwdConfirm!='' && pwdConfirm!=pwd){
        return false;
    }
    return true;
}, "(登录密码与确认密码不一致)");


jQuery.validator.addMethod("isPhone", function(value, element) {
    var phone = $('#text_phone').val();
    if(phone!=''){
        var reg01 = /^(0|86|17951)?(13[0-9]|15[012356789]|17[01678]|18[0-9]|14[57])[0-9]{8}$/;
        var reg02 = /^(0[0-9]{2,3}\-)([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$/;
        if(!reg01.test(phone) && !reg02.test(phone)){
            return false
        }
    }
    return true;
}, "(请输入正确的联系方式)");

//表单验证*****************************
function validateForm() {
//validate方法参数可选
    return $("#myform").validate({
        rules: {
            text_enterpriseName: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_types: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_userName: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_phone: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                isPhone: true,
                // number: true, //必须是数字
            },
            text_cityCode: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_districtCode: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_townCode: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_address: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_iDCard: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_animal: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_loginID: {
                required: true,//必填
                minlength: 3, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_password: {
                required: true,//必填
                minlength: 6, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_passwordConfirm: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                passwordValidator: true
                // number: true, //必须是数字
            },
            text_animal_name: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            }
        },
//错误信息提示
        messages: {
            text_enterpriseName: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_types: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_userName: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_phone: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                isPhone: "(请输入正确的联系方式)"
                // number: true, //必须是数字
            },
            text_cityCode: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_districtCode: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_townCode: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_address: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_iDCard: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_animal: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_loginID: {
                required: "(必填)",
                minlength: "(至少三个字符)", //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_password: {
                required: "(必填)",
                minlength: "(至少六个字符)", //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_passwordConfirm: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                passwordValidator : "(登录密码与确认密码不一致)"
                // number: true, //必须是数字
            },
            text_animal_name: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // passwordValidator : "(登录密码与确认密码不一致)"
                // number: true, //必须是数字
            }
        },
    }).form();

}
